<?php

namespace Tests\NongShang;

use NongShang\NongShang;
use PHPUnit\Framework\TestCase;

class TestNongShang extends TestCase
{

    public function testGetPlan()
    {
        $equipmentAmount = 4000000.00;
        $plusAmount = 0.00;
        $downPayment = 0.00;
        $deposit = 400000.00;
        $residualValue = 0.00;
        $serviceAmount = 140000.00;
        $issues = 36;
        $ns = new NongShang($equipmentAmount, $plusAmount, $downPayment, $deposit, $residualValue, $serviceAmount, $issues);
        $repayments = [
            0.00,
            170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00,
            170000.00, 170000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00,
            130000.00, 130000.00, 130000.00, 130000.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00,
            83200.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00
        ];
        $ns->setRepayments($repayments);
        $plan = $ns->getPlan();
        var_dump($plan);
        self::assertIsArray($plan);
    }

    public function testGetOutsideRate()
    {
        $equipmentAmount = 4000000.00;
        $plusAmount = 0.00;
        $downPayment = 0.00;
        $deposit = 400000.00;
        $residualValue = 0.00;
        $serviceAmount = 140000.00;
        $issues = 36;
        $ns = new NongShang($equipmentAmount, $plusAmount, $downPayment, $deposit, $residualValue, $serviceAmount, $issues);
        $repayments = [
            0.00,
            170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00,
            170000.00, 170000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00,
            130000.00, 130000.00, 130000.00, 130000.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00,
            83200.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00
        ];
        $ns->setRepayments($repayments);
        $outsideRate = $ns->getOutsideRate();
        var_dump($outsideRate);
        self::assertEqualsWithDelta(0.1096, $outsideRate, 0.0001);
    }

    public function testGetInsideRate()
    {
        $equipmentAmount = 4000000.00;
        $plusAmount = 0.00;
        $downPayment = 0.00;
        $deposit = 400000.00;
        $residualValue = 0.00;
        $serviceAmount = 140000.00;
        $issues = 36;
        $ns = new NongShang($equipmentAmount, $plusAmount, $downPayment, $deposit, $residualValue, $serviceAmount, $issues);
        $repayments = [
            0.00,
            170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00,
            170000.00, 170000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00,
            130000.00, 130000.00, 130000.00, 130000.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00,
            83200.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00
        ];
        $ns->setRepayments($repayments);
        $insideRate = $ns->getInsideRate();
        var_dump($insideRate);
        self::assertEqualsWithDelta(0.1730, $insideRate, 0.0001);
    }

    public function testGetFlatRate()
    {
        $equipmentAmount = 4000000.00;
        $plusAmount = 0.00;
        $downPayment = 0.00;
        $deposit = 400000.00;
        $residualValue = 0.00;
        $serviceAmount = 140000.00;
        $issues = 36;
        $ns = new NongShang($equipmentAmount, $plusAmount, $downPayment, $deposit, $residualValue, $serviceAmount, $issues);
        $repayments = [
            0.00,
            170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00, 170000.00,
            170000.00, 170000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00, 130000.00,
            130000.00, 130000.00, 130000.00, 130000.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00,
            83200.00, 83200.00, 83200.00, 83200.00, 83200.00, 83200.00
        ];
        $ns->setRepayments($repayments);
        $flatRate = $ns->getFlatRate();
        var_dump($flatRate);
        self::assertEqualsWithDelta(0.0499, $flatRate, 0.0001);
    }

    public function testGetInsideXirr()
    {
        $equipmentAmount = 4000000.00;
        $plusAmount = 0.00;
        $downPayment = 0.00;
        $deposit = 400000.00;
        $residualValue = 0.00;
        $serviceAmount = 140000.00;
        $issues = 36;
        $ns = new NongShang($equipmentAmount, $plusAmount, $downPayment, $deposit, $residualValue, $serviceAmount, $issues);
        $repayments = [
            '2019-12-28' => 0.00,
            '2020-01-01' => 170000.00,
            '2020-02-02' => 170000.00,
            '2020-03-03' => 170000.00,
            '2020-04-04' => 170000.00,
            '2020-05-05' => 170000.00,
            '2020-06-06' => 170000.00,
            '2020-07-07' => 170000.00,
            '2020-08-08' => 170000.00,
            '2020-09-09' => 170000.00,
            '2020-10-10' => 170000.00,
            '2020-11-11' => 170000.00,
            '2020-12-12' => 170000.00,
            '2021-01-13' => 130000.00,
            '2021-02-14' => 130000.00,
            '2021-03-15' => 130000.00,
            '2021-04-16' => 130000.00,
            '2021-05-17' => 130000.00,
            '2021-06-18' => 130000.00,
            '2021-07-19' => 130000.00,
            '2021-08-20' => 130000.00,
            '2021-09-21' => 130000.00,
            '2021-10-22' => 130000.00,
            '2021-11-23' => 130000.00,
            '2021-12-24' => 130000.00,
            '2022-01-25' => 83200.00,
            '2022-02-26' => 83200.00,
            '2022-03-27' => 83200.00,
            '2022-04-28' => 83200.00,
            '2022-05-29' => 83200.00,
            '2022-06-30' => 83200.00,
            '2022-07-01' => 83200.00,
            '2022-08-02' => 83200.00,
            '2022-09-03' => 83200.00,
            '2022-10-04' => 83200.00,
            '2022-11-05' => 83200.00,
            '2022-12-06' => 83200.00
        ];
        $ns->setRepayments($repayments);
        $xirr = $ns->getInsideXirr();
        var_dump($xirr);
        self::assertIsFloat($xirr);
    }
}
